package com.yylany.service;

import com.yylany.mapper.TbCategoryMapper;
import com.yylany.pojo.TbCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class CategoryService {
    private TbCategoryMapper categoryMapper;

    @Autowired
    public CategoryService(TbCategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    /**
     * cat1_id : 0 ; cat1_id : 0     查找一级分类：父分类
     * 查找二级分类   cat1_id : 一级分类的id   cat1_id : 0
     * 查找三级分类 cat1_id : 一级分类的id   cat1_id :二级分类的id
     *
     * @param cat1_id 一级分类的id
     * @param cat2_id 二级分类的id
     * @return 返回匹配的分类信息
     */
    public List<TbCategory> findCategorys(Integer cat1_id, Integer cat2_id) {
        Example example = new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        // 添加所属一级分类条件
        criteria.andEqualTo("cat1Id", cat1_id);
        // 添加所属二级分类条件
        criteria.andEqualTo("cat2Id", cat2_id);
        return categoryMapper.selectByExample(example);
    }

}
